<template>
  <div>
    <el-card style="height: 80px">
      <div class="box-card-div">

        <el-form :inline="true" :model="order" ref="ruleForm" class="demo-form-inline">
          <el-form-item label="订单编号" prop="orderNum">
          <el-select style="width: 200px;" size="small" v-model="order.orderNum" placeholder="请输入订单编号">
              <el-option
                v-for="item in orderNum"
                :key="item.id"
                :label="item.orderNum"
                :value="item.orderNum">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户名字" prop="yongHuName">
            <el-input v-model="order.yongHuName" size="small" placeholder="请选择用户名字"></el-input>
          </el-form-item>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

          <el-form-item>
            <el-button type="success" plain size="small" @click="onSearch">查询</el-button>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <el-button type="warning" plain size="small" @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
          <br>
        </el-form>
      </div>
    </el-card>

    <div>
      <br>
      <el-button type="primary" plain size="small" @click="handleAddDialog = true">新增</el-button>
      <el-button type="danger" plain size="small" @click="handleDeleteAll">批量删除</el-button>

      <el-button type="primary" plain size="small" @click="uploadExcelDialog = true">导入</el-button>
      <el-button type="warning" plain size="small" @click="downloadExcel">导出</el-button>
      <el-button type="warning" size="small" @click="showChart">订单状态统计</el-button>
      <br>
    </div>

    <!--新增的弹框-->
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      style="margin-left: 300px; width: 1000px"
      :visible.sync="handleAddDialog"
    >
      <el-form
        :model="editOrder"
        label-width="120px"
        :rules="rules"
        ref="editOrder"
      >
        <!-- 订单编号 -->
        <el-form-item label="订单编号" prop="orderNum">
          <el-input
            v-model="editOrder.orderNum"
            placeholder="请输入订单编号"
            clearable
            :style="{width: '100%'}"
          ></el-input>
        </el-form-item>

        <!-- 用户ID -->
        <el-form-item label="用户ID" prop="yongHuId">
          <el-input
            v-model="editOrder.yongHuId"
            placeholder="请输入用户ID"
            clearable
            :style="{width: '100%'}"
          ></el-input>
        </el-form-item>

        <!-- 用户姓名 -->
        <el-form-item label="用户姓名" prop="yongHuName">
          <el-input
            v-model="editOrder.yongHuName"
            placeholder="请输入用户姓名"
            clearable
            :style="{width: '100%'}"
          ></el-input>
        </el-form-item>

        <!-- 订单状态 -->
        <el-form-item label="订单状态" prop="status">
          <el-select
            v-model="editOrder.status"
            placeholder="请选择订单状态"
            :style="{width: '100%'}"
          >
            <el-option label="已解决" value="Y"></el-option>
            <el-option label="未核销" value="N"></el-option>
          </el-select>
        </el-form-item>

        <!-- 预约时间 -->
        <el-form-item label="预约时间" prop="creatDate">
          <el-date-picker
            v-model="editOrder.creatDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择预约时间"
            :style="{width: '100%'}"
          ></el-date-picker>
        </el-form-item>

        <!-- 订单备注 -->
        <el-form-item label="订单备注" prop="orderBeiZhu">
          <el-input
            v-model="editOrder.orderBeiZhu"
            type="textarea"
            placeholder="请输入订单备注"
            :autosize="{minRows: 4, maxRows: 4}"
            :style="{width: '100%'}"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('editOrder')" size="small">取 消</el-button>
        <el-button type="primary" @click="submitForm('editOrder')" size="small">确 定</el-button>
      </div>
    </el-dialog>

    <!--上传execl弹窗-->
    <el-dialog
      title="上传预约订单数据"
      width="26%"
      :visible.sync="uploadExcelDialog"
      :close-on-click-modal="false"
    >
      <!--上传组件-->
      <el-upload
        class="upload-demo"
        drag
        action=""
        :before-upload="beforeUpload"
        :file-list="fileList"
        :http-request="uploadExcelFun"
        :on-change="handleChange"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="uploadExcelDialog = false">取 消</el-button>
        <el-button type="primary" @click="">确 定</el-button>
      </div>
    </el-dialog>
    <!--订单状态图表弹窗-->
    <el-dialog
      title="订单状态图表"
      :visible.sync="charsDialog"
      width="800px"
      height="800px"
      :close-on-click-modal="false"
    >
      <div id="statusChart" style="width: 100%; height: 400px;"></div>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="charsDialog = false">关闭</el-button>
      </div>
    </el-dialog>


    <el-table
      :data="tableData"
      stripe
      max-height="700"
      @selection-change="handleChoiceChange"
      style="width: 100%">
      <!-- 多选框 -->
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <!-- 序号 -->
      <el-table-column
        label="序号"
        prop="id"
        width="100">
      </el-table-column>

      <!-- 订单编号 -->
      <el-table-column
        prop="orderNum"
        width="120"
        label="订单编号">
      </el-table-column>

      <!-- 创建时间 -->
      <el-table-column
        prop="creatDate"
        label="创建时间">
      </el-table-column>

      <!-- 订单状态 -->
      <el-table-column
        label="订单状态">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status === 'Y' ? 'success' : 'danger'"
            disable-transitions>{{ scope.row.status === 'Y' ? '已解决' : '未核销' }}</el-tag>
        </template>
      </el-table-column>

      <!-- 订单备注 -->
      <el-table-column
        prop="orderBeiZhu"
        label="订单备注">
      </el-table-column>

      <!-- 用户ID -->
      <el-table-column
        prop="yongHuId"
        label="用户ID">
      </el-table-column>

      <!-- 用户名字 -->
      <el-table-column
        prop="yongHuName"
        label="用户名字">
      </el-table-column>

      <!-- 操作 -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="display: flex; align-items: center; margin-top: 30px; margin-left: 400px;">
      <el-pagination
        background
        @size-change="handleSizeChange"
        :page-sizes="[5, 7, 10]"
        @current-change="handleCurrentChange"
        :current-page="order.pageNum"
        :page-size="order.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <el-button style="margin-left: 10px" size="small" type="primary" plain @click="toPage">跳转</el-button>
    </div>


  </div>
</template>

<script>
import * as echarts from 'echarts'
import {
  getOrderNum, getStatusRate,
  queryAllYuYueOrderPage, uploadExcel,
  ZfYuYueOrderAdd,
  ZfYuYueOrderDelete,
  ZfYuYueOrderDeleteAll,
  ZfYuYueOrderUpdate
} from '@/api/zf/zf-yuyue-order'

export default {
  name: 'OrderManage',
  data() {
    return {
      title: '添加用户',
      tableData: [],
      yongHuName: [],
      handleAddDialog: false,
      uploadExcelDialog: false,
      charsDialog: false,
      handleChoice: [],
      orderNum: [],
      order: {
        pageSize: 7,
        pageNum: 1,
        yongHuName: '',
      },
      editOrder: {
        orderNum: '',
        yongHuId: '',
        yongHuName: '',
        status: 'Y',
        creatDate: '',
        orderBeiZhu: '',
      },
      rules: {
        orderNum: [{ required: true, message: '请输入订单编号', trigger: 'blur' }],
        id: [{ required: true, message: '请输入用户ID', trigger: 'blur' }],
        yongHuName: [{ required: true, message: '请输入用户姓名', trigger: 'blur' }],
        status: [{ required: true, message: '请选择订单状态', trigger: 'change' }],
        creatDate: [{ required: true, message: '请选择预约时间', trigger: 'change' }]
      },
      total: 0,
      loading: false,
      file: null,
      fileList: [],
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initstatusChart();
    });
  },
  methods: {
    // 查询全部订单
    queryAllOrder() {
      this.loading = true;
      queryAllYuYueOrderPage(this.order).then(ret => {
        this.tableData = ret.data.list;
        this.total = ret.data.total;
      });
    },
    handleSizeChange(size) {
      console.log('每页${size}条');
      this.order.pageSize = size;
      this.queryAllOrder();
    },
    handleCurrentChange(currentPage) {
      this.order.pageNum = currentPage;
      this.queryAllOrder();
    },
    toPage() {
      this.queryAllOrder();
    },
    onSearch() {
      this.queryAllOrder();
    },
    getOrderNum() {
      getOrderNum().then(ret => {
        console.log(ret.data)
        this.orderNum = ret.data;
      })
    },
    handleEdit(index, row) {
      this.title = "修改用户";
      this.editOrder = row;
      this.handleAddDialog = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title === "添加用户") {
            ZfYuYueOrderAdd(this.editOrder).then(ret => {
              if (ret.code === 20000) {
                this.$message({
                  type: 'success',
                  message: '添加成功!',
                  duration: 1000
                });
                this.queryAllOrder()
                this.editOrder = {};
                this.$refs[formName].resetFields();
              }
            })
          } else {
              ZfYuYueOrderUpdate(this.editOrder)
                .then(ret => {
                  if (ret.code === 20000) {
                    this.$message({
                      type: 'success',
                      message: '更新成功!',
                      duration: 1000
                    });
                    this.$refs[formName].resetFields();
                    this.resetForm(formName);
                  }
                })
          }
          this.handleAddDialog = false;
        } else {
          this.$message({
            type: 'error',
            message: '请检查是否输入正确!'
          });
          return false;
        }
      });
    },

    handleDelete(index, row) {
      this.$confirm('此操作将永久删除' + '"' + row.yongHuId + '：' + row.yongHuName + '"' + '该数据, 是否继续?', '删除用户!', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ZfYuYueOrderDelete(row.id).then(ret => {
          if (ret.code === 20000) {
            // 弹框
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.queryAllOrder();
          } else {
            this.$message({
              type: 'error',
              message: '删除失败，请稍后重试！'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleChoiceChange(val) {
      this.handleChoice = val.map(order => order.id);
      console.log(this.handleChoice);
    },
    handleDeleteAll() {
      if (this.handleChoice.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的数据!'
        });
        return;
      }
      this.$confirm('此操作将永久删除['+this.handleChoice+']数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ZfYuYueOrderDeleteAll(this.handleChoice).then(ret => {
          if (ret.code === 20000) {
            // 弹框
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.queryAllOrder();
          }else{
            this.$message({
              type: 'error',
              message:'删除失败，请稍后重试！'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    showChart() {
      this.charsDialog = true;
      this.$nextTick(() => {
        this.initstatusChart();
      });
    },
    initstatusChart() {
      const statusChart = echarts.init(document.getElementById('statusChart'));
      getStatusRate().then(ret => {
        const option = {
          title: { text: '订单状态统计图' },
          tooltip: {},
          xAxis: { data: ['已解决', '未核销'] },
          yAxis: {},
          series: [{
            name: '数量',
            type: 'bar',
            data: [ret.data.Y || 0, ret.data.N || 0]
          }]
        };
        statusChart.setOption(option);
      });
    },
    downloadExcel() {
      // 确认框
      this.$confirm('确认导出订单数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 使用js原生请求方式下载文件
        window.location.href = 'http://localhost:9999//zf/download/yuYueOrder';
      }).catch(() => {
        this.$message.info('已取消导出')
      });
    },
    // 上传前的回调，用于清空上次上传的文件列表
    beforeUpload(file) {
      this.fileList = [];
      return true;
    },
    uploadExcelFun(){
      const file = this.file;
      const formData = new FormData();
      formData.append('file', file); //file固定名
      uploadExcel(formData).then(ret => {
        this.$message({
          type: 'success',
          message: '上传成功!',
          duration: 1000
        });
        // 上传成功后添加到文件列表
        this.fileList.push({
          name: file.name,
        }); // 上传成功后添加到文件列表
        this.uploadExcelDialog = false;
        this.pageNum = 1;
        this.queryAllOrder();
      });
    },
    handleChange(file){
      this.file = file.raw;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.handleAddDialog = false;
      this.editOrder = {
        status: 'Y',
        creatDate: '',
      };
      this.title = "添加用户";
      this.queryAllOrder();
    },
  },
  created() {
    this.queryAllOrder();
    this.getOrderNum();
  },
}
</script>

<style scoped lang="scss">

</style>
